<template>
  <div class="roleManagement-container">
    <el-divider content-position="left">
      <el-tag>{{ clazz.title }}</el-tag>
    </el-divider>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="id"
        label="id"
      ></el-table-column>

      <el-table-column
        show-overflow-tooltip
        prop="title"
        label="名称"
      ></el-table-column>

      <el-table-column
        show-overflow-tooltip
        prop="remark"
        width="50px"
        label="备注"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="80">
        <template #default="{ row }">
          <el-button type="text" @click="handleJoinTest(row)">
            进入考试
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="选择试卷" :visible.sync="dialogVisible">
      <el-table :data="gridData">
        <el-table-column property="title" label="标题"></el-table-column>
        <el-table-column
          property="remark"
          label="备注"
          width="120"
        ></el-table-column>
        <el-table-column property="操作" label="操作" width="80">
          <template #default="{ row }">
            <el-button type="text" @click="handleStart(row)">
              开始考试
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
  import * as api from '/src/api/course'
  import * as clazzapi from '/src/api/clazz'
  import { defaultPageSize } from '/src/config'
  import { answerSheetByPaper } from '@/api/answerSheet'

  export default {
    name: 'Management',
    components: {},
    data() {
      return {
        idkey: 'id',
        clazz: {},
        gridData: [],
        list: null,
        dialogVisible: false,
        listLoading: true,
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: defaultPageSize,
          order: {
            //  sort: 'asc'
          },
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      async handleStart(row) {
        const resp = await answerSheetByPaper(row.id)

        let answerSheetid = resp.data.id
        let { href } = this.$router.resolve({
          path: `/testing/${answerSheetid}`,
        })
        window.open(href, '_blank')
      },
      async handleJoinTest(row) {
        let resp = await api.getAllPaper(row.id)
        console.log(resp.data)
        this.gridData = resp.data
        this.dialogVisible = true
      },
      handleDelete(row) {
        if (row[this.idkey]) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row[this.idkey] })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item[this.idkey]).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },

      async fetchData() {
        this.listLoading = true

        const resp = await api.getSelfAll()
        const data = resp.data

        this.list = data

        const resp2 = await clazzapi.getSelfClazz()

        this.clazz = resp2.data

        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
